
package com.android.fitnesscenter.view.client;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.FitnessCenterContants.ResultCode;
import com.android.fitnesscenter.utils.ConfigProperties;
import com.android.fitnesscenter.utils.Loger;
import com.android.fitnesscenter.utils.ToastHelper;
import com.android.fitnesscenter.utils.ViewHelper;
import com.android.fitnesscenter.view.BaseActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class ClientNewPasswordActivity extends BaseActivity {

    @ViewInject(R.id.edtxt_old_password)
    private EditText edtxt_old_password;

    @ViewInject(R.id.edtxt_new_password)
    private EditText edtxt_new_password;

    @ViewInject(R.id.btn_ok)
    private Button btn_ok;

    private String secret, username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_password_layout);
        ViewUtils.inject(this);
        initTitleBar(getString(R.string.txt_get_password));
        edtxt_old_password.setHint(getString(R.string.txt_new_password_hint));
        edtxt_new_password.setHint(getString(R.string.txt_new_password_again_hint));

        Intent intent = getIntent();
        if (intent != null) {
            secret = intent.getStringExtra("secret");
            username = intent.getStringExtra("username");
        }
    }

    @OnClick(R.id.btn_ok)
    public void btn_ok(View view) {
        String password = "";
        if (edtxt_old_password != null && edtxt_old_password.getText() != null) {
            password = edtxt_old_password.getText().toString();
        }
        if (TextUtils.isEmpty(password)) {
            ToastHelper.getInstance().showShortMsg("请输入密码!");
            return;
        }
        if (edtxt_new_password != null && edtxt_new_password.getText() != null) {
            String npassword = edtxt_new_password.getText().toString();
            if (!TextUtils.equals(password, npassword)) {
                ToastHelper.getInstance().showShortMsg("两次密码不一致!");
                return;
            }
        }
        resetPassword(password);
    }

    private ProgressDialog mDialog;

    private void resetPassword(String password) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        params.addBodyParameter("username", username);
        params.addBodyParameter("secret", secret);
        params.addBodyParameter("password", password);

        String url = ConfigProperties.SERVICE_URL + "resetPassword.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                mDialog = new ProgressDialog(ClientNewPasswordActivity.this);
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (ClientNewPasswordActivity.this != null && !ClientNewPasswordActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        try {
                            String result = stringResponseInfo.result;
                            Loger.e("result is " + result);
                            JSONObject jsonObject = new JSONObject(result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                String msg = jsonObject.getString("message");
                                if (success) {
                                    ToastHelper.getInstance().showShortMsg(msg);
                                    backToClientLoginActivity();
                                    setResult(ResultCode.GET_PASSWORD);
                                    finish();
                                } else {
                                    new ViewHelper(ClientNewPasswordActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(ClientNewPasswordActivity.this).showBTNDialog("加载失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                if (ClientNewPasswordActivity.this != null && !ClientNewPasswordActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(ClientNewPasswordActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    @Override
    protected void titlebarMethod(int id) {

    }

}
